This page is about making some plotly examples.
library(tidyverse)
library(p8105.datasets)
library(plotly)
Let’s look at instacart data.
data("instacart")
instacart %>%
group_by(aisle) %>%
summarize(
n_obs = n()) %>%
arrange(-n_obs) %>%
filter(n_obs > 10000) %>%
mutate(aisle = forcats::fct_reorder(aisle, n_obs)) %>%
plot_ly(
x = ~aisle, y = ~n_obs, type = "bar",
color = "bule") %>%
layout(
title = "The number of items ordered in each aisle",
xaxis = list(title = ''),
yaxis = list(title = 'Number of items'))
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
instacart %>%
mutate(
department = fct_reorder(factor(department), add_to_cart_order)
) %>%
filter(add_to_cart_order <= 20) %>%
group_by(department) %>%
plot_ly(x = ~ department, y = ~ add_to_cart_order, type = "box") %>%
layout(
title = "The add to cart order for each department",
xaxis = list(title = 'Department'),
yaxis = list(title = 'Add to car order'))